<template>
  <van-button
    v-if="status"
    type="info"
    round
    size="mini"
    :loading="isLoading"
    @click="changeFollowStatus"
  >互相关注</van-button>
  <van-button
    v-else
    type="info"
    round
    icon="plus"
    size="small"
    :loading="isLoading"
    @click="changeFollowStatus"
  >{{ text }}</van-button>
</template>

<script>
import { toFollow, cancelFollow } from '@/api/user.js'
export default {
  name: 'MutualItem',
  props: {
    targetId: {
      type: [Object, Number, String],
      required: true
    },
    status: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      isLoading: false,
      text: '已关注'
    }
  },
  methods: {
    async changeFollowStatus() {
      this.isLoading = true
      try {
        if (this.text === '已关注' || this.status) {
          // 取消关注
          await cancelFollow(this.targetId)
          console.log(1);
          this.text = '关注'
          this.$emit('change')
        } else {
            // 再次关注
            await toFollow(this.targetId)
            this.text = '已关注'
        }
      } catch (err) {
        console.log(err)
        this.$toast.fail('操作失败')
      }
      this.isLoading = false
    }
  }
}
</script>

<style scope lang="less"></style>
